<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-form-item label="项目编号" prop="name">
          <el-input
            v-model="queryParams.projectName"
            placeholder="请输入项目名称"
            clearable

          />
        </el-form-item>
        <el-form-item label="项目编号" prop="belongUnit">
          <el-input
            v-model="queryParams.projectNo"
            placeholder="请输入项目编号"
            clearable
          />
        </el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cooperateList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="项目编号" align="center" prop="projectNo" />
      <el-table-column label="项目名称" align="center" prop="projectName" />
      <el-table-column label="实施负责人" align="center" prop="implementLeader" />
      <el-table-column label="协作方" align="center" prop="collaborateCompany" />
      <el-table-column label="启动时间" align="center" prop="startTime" />
      <el-table-column label="完成时间" align="center" prop="completeTime" />
      <el-table-column label="费用" align="center" prop="fee" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="实付费用" align="center" prop="realityFee" />
      <el-table-column label="付款状态" align="center" prop="feeStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_fee_status" :value="scope.row.feeStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="说明" align="center" prop="remark" :formatter="stateFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.feeStatus !== '1'"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleSelect(scope.row)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改采购与合作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px"  :inline="true">
        <el-form-item label="项目编号" prop="name">
          <el-input v-model="form.projectNo" :disabled="true" />
        </el-form-item>
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="form.projectName" :disabled="true" />
        </el-form-item>
        <el-form-item label="实施负责人" prop="name">
          <el-input v-model="form.implementLeader"  :disabled="true"/>
        </el-form-item>
        <el-form-item label="协作方" prop="name">
          <el-input v-model="form.collaborateCompany" :disabled="true" />
        </el-form-item>
        <el-form-item label="启动时间" prop="name">
          <el-input v-model="form.startTime" :disabled="true" />
        </el-form-item>
        <el-form-item label="完成时间" prop="name">
          <el-input v-model="form.completeTime" :disabled="true" />
        </el-form-item>
        <el-form-item label="费用" prop="name">
          <el-input v-model="form.fee" :disabled="true" />
        </el-form-item>
        <el-form-item label="状态" prop="name">
          <el-input v-model="form.status" :disabled="true" />
        </el-form-item>
        <el-form-item label="实付费用" prop="name" >
          <el-input v-model="form.realityFee"  :disabled="sign===0"/>
        </el-form-item>
        <el-form-item label="付款状态" prop="name">
          <el-select v-model="form.feeStatus" placeholder="请选择性别" style="width: 202.5px">
            <el-option
              v-for="dict in dict.type.sys_fee_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
              :disabled="sign===0"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="name">
          <el-input v-model="form.remark"  type="textarea" style="width: 500px;"   :disabled="sign===0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="sign===1">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button  @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProcure, getCooperate,addCooperate, updateCooperate } from "@/api/cooperate/cooperate";

export default {
  name: "procure",
  dicts: ['sys_fee_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 采购与合作表格数据
      cooperateList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 15,
      },
      //修改和查看标志位
      sign:0,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询采购与合作列表 */
    getList() {
      this.loading = true;
      listProcure(this.queryParams).then(response => {
        console.log(response.rows)
        this.cooperateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectId: null,
        realityFee: null,
        feeStatus: null,
        remark: null,
        procureCooperate: null,
        operateId: null,
        departmentId: null,
        departmentName: null,
        organizationId: null,
        organizationName: null,
        creationTime: null,
        operateTime: null
      };
      this.resetForm("form");
    },
    //做字符显示限制
    stateFormat(row, column, cellValue) {
      if (!cellValue) return ''
      if (cellValue.length > 10) {       //最长固定显示10个字符
        return cellValue.slice(0, 10) + '...'
      }
      return cellValue
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    //查看
    handleSelect(row){
      this.reset();
      const id = row.id || this.ids
      getCooperate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "查看";
        this.sign=0;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCooperate(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改采购信息";
        this.sign=1;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCooperate(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCooperate(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cooperate/cooperate/exportProcure', {
        ...this.queryParams
      }, `cooperate_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
